<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="d" uri="http://dgg.net/jsp/jstl/core" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<%--资质匹配页面--%>
<div class="body-main">
    <input type="hidden" id="layerIndex">
    <%--生产订单ID--%>
    <input type="hidden" name="scOrderId" id="scOrderId" value="${scOrderId}">
    <div id="titleSearchId" class="titleSearch">
        <div class="layui-inline">
            <label>资质类型：</label>
            <div class="layui-input-inline">
                <select class="select-style" name="certificateType" id="certificateType">
                    <option value="">请选择</option>
                    <c:forEach items="${typeList}" var="cerType">
                        <option data-id="${cerType.id}" value="${cerType.id}">${cerType.certificateName}</option>
                    </c:forEach>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label>资质名称：</label>
            <div class="layui-input-inline">
                <select  name="certificateName" class="select-style" id="certificateName" autocomplete="off">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label>资质等级：</label>
            <div class="layui-input-inline">
                <select class="select-style" name="certificateLevel" id="certificateLevel">
                    <option value="">请选择</option>
                    <c:forEach items="${certificateLevels}" var="level">
                        <option value="${level.code}">${level.name}</option>
                    </c:forEach>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label>适用范围：</label>
            <div class="layui-input-inline">
                <select class="select-style" name="applicationScope">
                    <option value="">全部</option>
                    <c:forEach items="${applicationScopes}" var="scope">
                        <option value="${scope.code}">${scope.name}</option>
                    </c:forEach>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <input id="matchFlag" value="">
            <button class="layui-btn layui-btn-mini layui-btn-normal" onclick="matchSearch()">搜索</button>
            <button class="layui-btn layui-btn-mini layui-btn-primary" onclick="clean()">清空</button>
        </div>
    </div>
    <div class="table-conter">
        <input type="hidden" id="cpNumber" value="${cpNumber}">
            <label class="inline yudingClass match-del">已预订资质：<span id="reservationNum">${cpNumber}</span>个</label>
            <span class="yudingClass"  onclick="cancelAdvanceBaseCredentialBySc('${scOrderId}',2)">×</span>
        <input type="hidden" id="rnNumber" value="${rnNumber}">
            <label class="inline yuyueClass match-del">已预约资质：<span id="appointmentNum">${rnNumber}</span>个</label>
            <span class="yuyueClass" onclick="deleteProductReservationBySc('${scOrderId}',2)">×</span>
        <div class="fr">
            <a href="javascript:void(0)" class="layui-btn layui-btn-mini" onclick="qualificationAppointment('${scOrderId}')">预约</a>
            <a href="javascript:void(0)" class="layui-btn layui-btn-mini" onclick="qualificationAdvanceMatch('${scOrderId}')">匹配</a>
        </div>
    </div>
    <table id="myTable" class="cell-border crm-table">
    </table>
</div>
<script type="text/javascript">
    var matchFlag = 0;
    var qualifiMatchTable;
    $(document).ready(function () {
        if (parseInt($('#cpNumber').val()) > 0){
            $('.yudingClass').show();
        } else {
            $('.yudingClass').hide();
        }
        if (parseInt($('#rnNumber').val()) > 0){
            $('.yuyueClass').show();
        } else {
            $('.yuyueClass').hide();
        }
        qualifiMatchTable = $('#myTable').dataTableExtend({
                "ajax": {
                    "url": "${rootUrl}/zs/credential/qualification/find_match_data.do",
                    "type": "post"
                }, "fnServerParams": function (aoData) {
                //搜索条件
                aoData.certificateType = $('[name="certificateType"]').val();
                aoData.certificateName = $('[name="certificateName"]').val();
                aoData.certificateLevel = $('[name="certificateLevel"]').val();
                aoData.certificateArea = $('[name="certificateArea"]').val();
                aoData.applicationScope = $('[name="applicationScope"]').val();
                aoData.matchFlag=matchFlag;
                },
            "fnInitComplete": function(settings, json) {
                //datatable加载完后重定位弹窗
                dgg.relocation($('#layerIndex').val());
            },
                "scrollX": false,//是否开启横项滚动
                "aoColumns": [
                    {
                        "title":"序号",
                        "orderable": false,
                        "render": function (data, type, full, meta) {
                            return meta.row + 1;
                        }
                    },
                    {
                        "title":"资质编号",
                        "orderable": false,
                        "data": "certificateNo",
                        "render": function (data, type, full, meta) {
                            return "<a href='javascript:void(0);'  class='layui-table-link' onclick='qualificationDetails(\"" + data + "\",\"" + full.id + "\")'>" + data + "</a>";
                        }
                    },
                    {
                        "title":"资质类型",
                        "data": "certificateType",
                        "orderable": false
                    },
                    {
                        "title":"所属企业名",
                        "data": "companyName",
                        "orderable": false
                    },
                    {
                        "title":"资质名称",
                        "data": "certificateSpecialtys",
                        "orderable": false,
                        "render": function (data, type, row, meta) {
                            var htm = '<p class="line2 remarkAlertClass" style="min-width: 120px" title="' + data + '">' + data + '</p>';
                            return htm;
                        }
                    },
                    {
                        "title":"资质等级",
                        "data": "certificateLevel",
                        "orderable": false
                    },
                    {
                        "title":"成本价格（元）",
                        "data": "receiptPrice",
                        "orderable": false
                    },
                    {
                        "title":"适用范围",
                        "data": "applicationScope",
                        "orderable": false
                    },
                    {
                        "title":"操作",
                        "orderable": false,
                        "data": {},
                        "render": function (data, type, row, meta) {
                            var operation = '<a class="layui-table-link" href="javascript:void(0)" onclick="advanceCertificate(\'' + row.id + '\')">预定</a>';
                            return operation;
                        }
                    }
                ]
            });

    });
    //根据资质类型查资质名称
    $("#certificateType").on("change",function () {
        findCertificateName("120px","#certificateType","#certificateName","ZZ_CREDENTIAL");
    });
    // //根据资质名称查资质等级
    // $("#certificateName").on("change",function () {
    //     findCertificateLevel("#certificateName","#certificateLevel");
    // });
    //搜索
    function matchSearch() {
        var certificateType = $("#certificateType").val();
        if (checkIsEmpty(certificateType)){
            DGG.Warning("请选择资质类型!");
            return;
        }
        var certificateName = $("#certificateName").val();
        if (checkIsEmpty(certificateName)){
            DGG.Warning("请选择资质名称!");
            return;
        }
        var certificateLevel = $("#certificateLevel").val();
        if (checkIsEmpty(certificateLevel)){
            DGG.Warning("请选择资质等级!");
            return;
        }
        matchFlag = 1;//把参数改了
        qualifiMatchTable.ajax.reload();
        setTimeout(function () {
            dgg.relocation($('#layerIndex').val());
        },300)
    }

    // 清空搜索
    function clean() {
        var $s = $('#titleSearchId');
        $s.find('input').val('');
        $s.find('select').val('');
        $s.find('select').val(null).trigger("change");
        $s.find('input').attr("data-id", '');
    }

    //预定证书/资质
    function advanceCertificate(certificateId) {
        var scOrderId = $("#scOrderId").val();//生产订单id
        DGG.Confire("确定预定此资质吗？", '预定资质', function () {
            DGG.loading();
            $.ajax({
                type: 'POST',
                data: {certificateId:certificateId,scOrderId:scOrderId,type:2},//type 1是证书,2是资质
                url: sysInfo.basePath + '/zzsc/inprocess/save_certificate_advance.do',
                dataType: 'JSON',
                success: function (data) {
                    DGG.loading('close');
                    if (data.code != 0) {
                        DGG.Errors(data.msg);
                    } else {
                        DGG.Succeed("操作成功!");
                        qualifiMatchTable.ajax.reload();
                        $.ajax({//异步获取已预定证书数量
                            type: 'POST',
                            data: {scOrderId:scOrderId,type:2},//type 1是证书,2是资质
                            url: sysInfo.basePath + '/zzsc/inprocess/query_advance_match_number.do',
                            dataType: 'JSON',
                            success: function (data) {
                                if (data.code != 0) {
                                    $('.yudingClass').hide();
                                    $('.yuyueClass').hide();
                                } else {
                                    var cpNumber = data.data.cpNumber;
                                    if (parseInt(cpNumber) > 0){//预定
                                        $('.yudingClass').show();
                                        $('#reservationNum').text(cpNumber);
                                    }else {
                                        $('.yudingClass').hide();
                                    }
                                    var rnNumber = data.data.rnNumber;
                                    if (parseInt(rnNumber) > 0){
                                        $('.yuyueClass').show();
                                        $('#appointmentNum').text(rnNumber);
                                    } else {
                                        $('.yuyueClass').hide();
                                    }
                                }
                            }
                        });
                    }
                },
                error: function () {
                }
            });
        });
    }

</script>

